﻿using System.Windows;
using Microsoft.Phone.Scheduler;

using Microsoft.Phone.Shell;
using Microsoft.Phone.Info;
using System;
using System.Linq;
using System.Reflection;

namespace MobiSms.TaskAgent
{
    public class ScheduledAgent : ScheduledTaskAgent
    {
        private static volatile bool _classInitialized;

        public ScheduledAgent()
        {
            if (!_classInitialized)
            {
                _classInitialized = true;

                Deployment.Current.Dispatcher.BeginInvoke(delegate
                {
                    Application.Current.UnhandledException += ScheduledAgent_UnhandledException;
                });
            }
        }

        private void ScheduledAgent_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            if (System.Diagnostics.Debugger.IsAttached)
            {
                System.Diagnostics.Debugger.Break();
            }
        }

        protected override void OnInvoke(ScheduledTask task)
        {
            //if (task is PeriodicTask)
            //    toastTitle = "皖讯：";
            //else if (task is ResourceIntensiveTask)
            //    toastTitle = "ResourceIntensiveTask";
            
            Assembly assembly = Assembly.Load("MobiSms.ViewModel");
            Type replayViewModel = assembly.GetType("MobiSms.ViewModel.ReplyViewModel");

            ConstructorInfo constructor = replayViewModel.GetConstructor(Type.EmptyTypes);
            object classObject = constructor.Invoke(new object[] { });

            MethodInfo getCountMethod = replayViewModel.GetMethod("RequestReply");
            if (getCountMethod.IsStatic)
            {
                getCountMethod.Invoke(null, null);
            }
            else
            {
                getCountMethod.Invoke(classObject, null);
            }


            #region Unused Code
            /*
            PropertyInfo countProperty = replayViewModel.GetProperty("Count");
            int count = Convert.ToInt32(countProperty.GetValue(classObject, null));

            // 弹出 Toast
            /*
            ShellToast toast = new ShellToast();
            toast.Title = "皖讯：";
            toast.Content = string.Format("收到 {0} 条回复；", count);
            toast.NavigationUri = new Uri("/Views/BackgroundAgentDemo.xaml?param=abc&param2=xyz", UriKind.Relative);
            toast.Show();
             */

            // 更新应用程序磁贴的 Badge
            /*
            ShellTile applicationTile = ShellTile.ActiveTiles.First();
            StandardTileData newTile = new StandardTileData
            {
                Count = count
            };
            applicationTile.Update(newTile);
            */
            
            #endregion
#if DEBUG
            // 15 秒后执行“task.Name”任务
            ScheduledActionService.LaunchForTest(task.Name, TimeSpan.FromSeconds(5));
#endif

            NotifyComplete();
        }
    }
}
